import string
import copy
import scipy
import Tkinter, tkFileDialog
import numpy as np
import pandas as pd
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
import os
import sys
import matplotlib.units
sys.path.append(os.path.abspath("C:\Users\Scherer Lab E\Documents\GitHub\Python_Data_Analysis"))
from common_functions import *
%matplotlib inline
#import seaborn as sns
'''Over Glass Experiments'''
glass_ramp_r,file_path_1=import_mosaic_trajectories(file_path="J:/Pat's Projects/Dynamical Phase Transition/Mosaic Trajectories/Mov_11121418.xls")
'''Over Plate Experiments'''
plate_ramp_r,file_path_2=import_mosaic_trajectories(file_path="J:/Pat's Projects/Dynamical Phase Transition/Mosaic Trajectories/Mov_11121401.xls")
plate_L_neg_1,file_path_2=import_mosaic_trajectories(file_path="J:/Pat's Projects/Dynamical Phase Transition/Mosaic Trajectories/Mov_11121403.xls")
'''Flip the y axis for all the images, For all the Mov_011514** videos they
all have a height of 380px'''
image_height=380
glass_ramp=y_axis_flip(glass_ramp_r,image_height)
plate_ramp=y_axis_flip(plate_ramp_r,image_height)
plate_L_neg_1=y_axis_flip(plate_L_neg_1,image_height)
'''Fit all the data to circles'''
xf_gl,yf_gl,rf_gl=least_sq_fit_circle(glass_ramp)
xf_pl,yf_pl,rf_pl=least_sq_fit_circle(plate_ramp)
xf_pl_L_neg_1,yf_pl_L_neg_1,rf_pl_L_neg_1=least_sq_fit_circle(plate_L_neg_1)
'''Create the Polar Coordinate DataFrame'''
polar_coor_data_frame(glass_ramp, xf_gl, yf_gl)
polar_coor_data_frame(plate_ramp, xf_pl, yf_pl)
polar_coor_data_frame(plate_L_neg_1, xf_pl_L_neg_1, yf_pl_L_neg_1)
'''Find longest trajectory in each experiment'''
lng_gl = find_longest_traj(glass_ramp)
lng_pl1 = find_longest_traj(plate_ramp)
lng_pl2 = find_longest_traj(plate_L_neg_1)
'''Define a function to easily split the trajectories into 600 frame chunks.
This is roughly the number of frames spent for each L'''
def split_single_traj_600_frames(data_frame):
df_list=[]
for div in range(len(parts)-1):
df_list.append(data_frame[(data_frame['frame']>=parts[div])&(data_frame['frame']<=parts[div+1])])
return df_list
'''Split each trajectory into it's own L'''
parts=[0,1100,2100,3100,4100,5100,6100,7100,8100,9100,10100,11000]
sp_gl=split_single_traj_600_frames(lng_gl)
parts=[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000]
sp_pl=split_single_traj_600_frames(lng_pl1)
def plot_velocity_around_ring(data_frame, t, mag_slider=1.6, hist_range=None):
um_conv=6.5/60/mag_slider/2
copy = data_frame.copy().drop_duplicates(subset=['frame','track id'])
dist = lambda x: (((x.shift(-t)['x pos']-x.shift(t)['x pos'])**2 + (x.shift(-t)['y pos']-x.shift(t)['y pos'])**2)**(0.5))/2.0
grouped = copy.groupby(['track id'])
# Need to stack if only one group
if len(grouped) == 1:
result = copy.groupby(['track id']).apply(dist)
result = result.stack()
else:
result = copy.groupby(['track id']).apply(dist)
copy['displacement'] = result.reset_index(level='track id').drop('track id', axis=1)
copy = copy.dropna(axis=0, subset=['displacement'])
#print "total tracks "+str(copy['track id'].max())
copy['velocity'] = copy['displacement']*um_conv*90.0
#print copy.velocity.describe()
hist, xedges, yedges = np.histogram2d(copy['theta'], copy['velocity'], bins=(20,20), range=hist_range)
theta, r = np.meshgrid(xedges,yedges, indexing='ij')
#ax = plt.subplot(111)
plt.scatter(copy['theta'], copy['velocity'])
#ax.set_theta_offset(np.pi*90/180.0)
#plt.show()
Ls = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
plt.figure(figsize=(8,20))
for idx,v in enumerate(sp_gl):
plt.subplot(6,2,idx+1)
plot_velocity_around_ring(v,1, hist_range=[[0,360],[0,0.5]])
plt.title('Over Glass L='+str(Ls[idx]))
plt.xlabel('Angle Position on Ring (degrees)')
plt.ylabel('Velocity (um/sec)')
plt.xlim([0,360])
plt.ylim([0,80])
plt.tight_layout()
plt.show()
plt.figure(figsize=(8,20))
for idx,v in enumerate(sp_pl):
plt.subplot(6,2,idx+1)
plot_velocity_around_ring(v,1, hist_range=[[0,360],[0,2]])
plt.title('Over Plate L='+str(Ls[idx]))
plt.xlabel('Angle Position on Ring (degrees)')
plt.ylabel('Velocity (um/sec)')
plt.xlim([0,360])
plt.ylim([0,190])
plt.tight_layout()
plt.show()